package com.yinp.proapp.module.im.activity;

import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.yinp.proapp.R;
import com.yinp.proapp.activity.bean.MyUserInfo;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.databinding.ActivityImBinding;
import com.yinp.proapp.module.im.fragment.ImFragmentOne;
import com.yinp.proapp.module.im.fragment.ImFragmentThree;
import com.yinp.proapp.module.im.fragment.ImFragmentTwo;
import com.yinp.proapp.utils.GlideUtils;
import com.yinp.proapp.utils.StatusBarUtil;

import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Message;
import io.rong.message.TextMessage;

/**
 * 测试融云im的界面
 */
public class ImActivity extends AppBaseActivity<ActivityImBinding> {

    private Fragment curFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    private ImFragmentOne imFragmentOne;
    private ImFragmentTwo imFragmentTwo;
    private ImFragmentThree imFragmentThree;

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg);
        bd.header.headerCenterTitle.setText("聊天");
        GlideUtils.into(mContext, R.mipmap.add_white2, bd.header.ivImg);
        bd.bottomRgp.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_one:
                    bd.header.ivImg.setVisibility(View.VISIBLE);
                    changFragment(1);
                    break;
                case R.id.rb_two:
                    bd.header.ivImg.setVisibility(View.GONE);
                    changFragment(2);
                    break;
                case R.id.rb_three:
                    bd.header.ivImg.setVisibility(View.GONE);
                    changFragment(3);
                    break;
            }
        });

        manager = getSupportFragmentManager();
        login();
        listenMessageAll();
        changFragment(1);
    }

    @Override
    public void onClick(View v) {
        if (v == bd.header.headerBackImg) {
            finish();
        }
    }

    private void login() {
        MyUserInfo myUserInfo = MyUserInfo.getCurrentUser(MyUserInfo.class);
        RongIMClient.connect(myUserInfo.getToken(), 0, new RongIMClient.ConnectCallback() {
            @Override
            public void onSuccess(String s) {
                Log.d(TAG, "onSuccess:im登录成功 ");
            }

            @Override
            public void onError(RongIMClient.ConnectionErrorCode connectionErrorCode) {
                Log.d(TAG, "onError: im登录失败");
            }

            @Override
            public void onDatabaseOpened(RongIMClient.DatabaseOpenStatus databaseOpenStatus) {
                Log.d(TAG, "onDatabaseOpened: ");
            }
        });
    }

    private void listenMessageAll() {
        RongIMClient.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageWrapperListener() {
            @Override
            public boolean onReceived(Message message, int i, boolean b, boolean b1) {
                Log.d(TAG, "onReceivedMessage: " + message);
                TextMessage textMessage = (TextMessage) message.getContent();
                return false;
            }
        });
    }

    private void changFragment(int checkedId) {
        transaction = manager.beginTransaction();
        switch (checkedId) {
            case 1:
                if (imFragmentOne == null) {
                    imFragmentOne = new ImFragmentOne();
                    transaction.add(R.id.fl_content, imFragmentOne);
                }
                if (curFragment != null) {
                    transaction.hide(curFragment);
                }
                curFragment = imFragmentOne;
                transaction.show(curFragment);
                transaction.commitNow();
                break;
            case 2:
                if (imFragmentTwo == null) {
                    imFragmentTwo = new ImFragmentTwo();
                    transaction.add(R.id.fl_content, imFragmentTwo);
                }
                if (curFragment != null) {
                    transaction.hide(curFragment);
                }
                curFragment = imFragmentTwo;
                transaction.show(curFragment);
                transaction.commitNow();
                break;
            case 3:
                if (imFragmentThree == null) {
                    imFragmentThree = new ImFragmentThree();
                    transaction.add(R.id.fl_content, imFragmentThree);
                }
                if (curFragment != null) {
                    transaction.hide(curFragment);
                }
                curFragment = imFragmentThree;
                transaction.show(curFragment);
                transaction.commitNow();
                break;
        }
    }

    /**
     * 功能选项，比如添加好友之类的
     */
    private void functionDialog() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RongIMClient.setOnReceiveMessageListener(null);
    }
}